package com.simdo.g768.SlideMenu;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.g768.Constant.ConnectConstant;
import com.simdo.g768.Bean.object.Cell_info;
import com.simdo.g768.Bean.object.Dev_info;
import com.simdo.g768.Bean.object.Heartbeat;
import com.simdo.g768.MainActivity;
import com.simdo.g768.R;
import com.simdo.g768.Util.AppLog;
import com.simdo.g768.Util.Battery;

import java.text.MessageFormat;
import java.util.List;

public class SlideLeftFragment extends Fragment implements View.OnClickListener {

    private View mView;
    TextView[] tv_cell;
    ImageView[] iv_cell;
    TextView tv_dev_run_state, tv_dev_work_mode, tv_dev_name, tv_dev_ip, tv_dev_temp, tv_dev_voltage, tv_dev_time_state, tv_dev_gps_state, tv_dev_air_state, tv_dev_run_time;
    public SlideLeftFragment() {

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) initView(inflater, container);
        // 用户配置自动布控的基站列表
        return mView;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        mView = inflater.inflate(R.layout.pager_left_slide, container, false);

        ImageView iv_cell_1 = mView.findViewById(R.id.iv_cell_1);
        ImageView iv_cell_2 = mView.findViewById(R.id.iv_cell_2);
        ImageView iv_cell_3 = mView.findViewById(R.id.iv_cell_3);
        ImageView iv_cell_4 = mView.findViewById(R.id.iv_cell_4);
        ImageView iv_cell_5 = mView.findViewById(R.id.iv_cell_5);
        ImageView iv_cell_6 = mView.findViewById(R.id.iv_cell_6);

        iv_cell = new ImageView[]{iv_cell_1, iv_cell_2, iv_cell_3, iv_cell_4, iv_cell_5, iv_cell_6};

        TextView tv_cell_1 = mView.findViewById(R.id.tv_cell_1);
        TextView tv_cell_2 = mView.findViewById(R.id.tv_cell_2);
        TextView tv_cell_3 = mView.findViewById(R.id.tv_cell_3);
        TextView tv_cell_4 = mView.findViewById(R.id.tv_cell_4);
        TextView tv_cell_5 = mView.findViewById(R.id.tv_cell_5);
        TextView tv_cell_6 = mView.findViewById(R.id.tv_cell_6);
        tv_cell = new TextView[]{tv_cell_1, tv_cell_2, tv_cell_3, tv_cell_4, tv_cell_5, tv_cell_6};

        tv_dev_run_state = mView.findViewById(R.id.tv_dev_run_state);
        tv_dev_work_mode = mView.findViewById(R.id.tv_dev_work_mode);
        tv_dev_name = mView.findViewById(R.id.tv_dev_name);
        tv_dev_ip = mView.findViewById(R.id.tv_dev_ip);
        tv_dev_ip = mView.findViewById(R.id.tv_dev_ip);
        tv_dev_temp = mView.findViewById(R.id.tv_dev_temp);
        tv_dev_voltage = mView.findViewById(R.id.tv_dev_voltage);
        //tv_dev_time_state = mView.findViewById(R.id.tv_dev_time_state);
        tv_dev_gps_state = mView.findViewById(R.id.tv_dev_gps_state);
        tv_dev_air_state = mView.findViewById(R.id.tv_dev_air_state);
        tv_dev_run_time = mView.findViewById(R.id.tv_dev_run_time);

        TextView tv_fan_close = mView.findViewById(R.id.tv_fan_close);
        TextView tv_fan_medium = mView.findViewById(R.id.tv_fan_medium);
        TextView tv_fan_fast = mView.findViewById(R.id.tv_fan_fast);
        TextView tv_fan_auto = mView.findViewById(R.id.tv_fan_auto);
        TextView[] tv_fan = new TextView[]{tv_fan_close, tv_fan_medium, tv_fan_fast, tv_fan_auto};
        for (int i = 0; i < tv_fan.length; i++) {
            int finalI = i;
            tv_fan[finalI].setBackgroundResource(R.drawable.btn_bg_cancel_2);
            tv_fan[finalI].setTextColor(Color.parseColor("#333333"));
            tv_fan[finalI].setOnClickListener(view -> {
                if (!MainActivity.getInstance().setFan(finalI * 50)) return;
                tv_fan[finalI].setBackgroundResource(R.drawable.btn_bg_ok);
                tv_fan[finalI].setTextColor(Color.parseColor("#ffffff"));

                for (int j = 0; j < tv_fan.length; j++){
                    if (j == finalI) continue;

                    tv_fan[j].setBackgroundResource(R.drawable.btn_bg_cancel_2);
                    tv_fan[j].setTextColor(Color.parseColor("#333333"));
                }
            });
        }

        mView.findViewById(R.id.tv_cat_freq_list).setOnClickListener(this);
        mView.findViewById(R.id.tv_cat_state).setOnClickListener(this);
        mView.findViewById(R.id.tv_cat_cell).setOnClickListener(this);

        updateConnectState(ConnectConstant.STATE_DISCONNECT);
    }

    /**
     * 更新连接状态
     *
     * @param state 101 连接中  103 已连接  100 未连接
     */
    public void updateConnectState(int state) {
        AppLog.I("updateConnectState  state = " + state);
        int color;
        String idle_str;
        int[] imageId = new int[6];

        if (state == ConnectConstant.STATE_CONNECTED) {
            idle_str = getString(R.string.idle);
            color = ContextCompat.getColor(MainActivity.getInstance().mContext, R.color.color_212121);
            imageId[0] = R.mipmap.icon_home_signal1;
            imageId[1] = R.mipmap.icon_home_signal2;
            imageId[2] = R.mipmap.icon_home_signal3;
            imageId[3] = R.mipmap.icon_home_signal4;
            imageId[4] = R.mipmap.icon_home_signal5;
            imageId[5] = R.mipmap.icon_home_signal6;
        } else {
            color = ContextCompat.getColor(MainActivity.getInstance().mContext, R.color.color_e65c5c);
            idle_str = getString(R.string.not_ready);
            imageId[0] = R.mipmap.icon_home_not_signal1;
            imageId[1] = R.mipmap.icon_home_not_signal2;
            imageId[2] = R.mipmap.icon_home_not_signal3;
            imageId[3] = R.mipmap.icon_home_not_signal4;
            imageId[4] = R.mipmap.icon_home_not_signal5;
            imageId[5] = R.mipmap.icon_home_not_signal6;
            initState();
        }

        for (int i = 0; i < 6; i++){
            tv_cell[i].setText(idle_str);
            tv_cell[i].setTextColor(color);

            iv_cell[i].setImageResource(imageId[i]);
        }

    }

    private void initState(){
        tv_dev_voltage.setText(MessageFormat.format("电压：{0}V  电量：{1}", 0, 0));
        tv_dev_temp.setText("温度：0℃");

        tv_dev_gps_state.setText(MessageFormat.format("{0}：{1}", getString(R.string.GPS_BeiDou),"未同步"));
        tv_dev_air_state.setText(MessageFormat.format("空口：\n1.{0}  2.{1}  3.{2}\n4.{3}  5.{4}  6.{5}" , "未同步", "未同步", "未同步", "未同步", "未同步", "未同步"));
    }

    /**
     * 更新工作加载进度条
     *
     * @param cell_id 小区id
     */
    public void updateProgress(int cell_id, String info, int color) {
        tv_cell[cell_id].setText(info);
        tv_cell[cell_id].setTextColor(color);
    }

    public void updateState(Heartbeat heartbeat){
        if (tv_dev_voltage == null) return;
        Dev_info devInfo = heartbeat.getDev_info();
        tv_dev_run_state.setText(MessageFormat.format("运行状态：{0}", devInfo.getDev_state() == 0 ? "正常" : "异常"));

        int work_mode = devInfo.getWork_mode();
        tv_dev_work_mode.setText(MessageFormat.format("支持模式：{0}", work_mode == 2 ? "定位/侦码/管控" : work_mode == 3 ? "侦码" : "管控/侦码"));

        tv_dev_name.setText(MessageFormat.format("设备名称：{0}", devInfo.getDev_name()));

        tv_dev_ip.setText(MessageFormat.format("设备IP：{0}", devInfo.getWifi_ip()));

        //tv_dev_time_state.setText(MessageFormat.format("时间状态：{0}", devInfo.getTime_sync_state() == 1 ? "同步" : "未同步"));

        int voltage = devInfo.getVoltage().get(1) * 310 / 210;
        Battery.build().handleVol(voltage);
        String percent = Battery.build().getPercent();
        // Vbat = Vsen x(210+R)/210
        tv_dev_voltage.setText(MessageFormat.format("电压：{0}V  电量：{1}%", voltage / 1000.0, percent));

        List<Integer> boardtemp = devInfo.getBoardtemp();
        tv_dev_temp.setText(MessageFormat.format("温度：{0}℃", boardtemp.get(1) / 1000.0));
        int gpsMode = devInfo.getGnss_select();
        tv_dev_gps_state.setText(MessageFormat.format("{0}：{1}", gpsMode == 0 ? getString(R.string.GPS_BeiDou) : gpsMode == 1 ? getString(R.string.GPS) : getString(R.string.BeiDou), devInfo.getGps_sync_state() == 1 ? "同步" : "未同步"));

        List<Cell_info> cellInfo = heartbeat.getCell_info();

        tv_dev_air_state.setText(MessageFormat.format("空口：\n1.{0}  2.{1}  3.{2}\n4.{3}  5.{4}  6.{5}"
                , cellInfo.get(0).getAsync_state() == 1 ? "同步" : "未同步"
                , cellInfo.get(1).getAsync_state() == 1 ? "同步" : "未同步"
                , cellInfo.get(2).getAsync_state() == 1 ? "同步" : "未同步"
                , cellInfo.get(3).getAsync_state() == 1 ? "同步" : "未同步"
                , cellInfo.get(4).getAsync_state() == 1 ? "同步" : "未同步"
                , cellInfo.get(5).getAsync_state() == 1 ? "同步" : "未同步"));

        int uptime = devInfo.getUptime();
        int day = uptime / (60 * 60 * 24);
        int hours = (uptime % (60 * 60 * 24)) / (60 * 60);
        int minutes = (uptime % (60 * 60)) / 60;
        int seconds = uptime % 60;
        tv_dev_run_time.setText(MessageFormat.format("设备运行时长：{0}天{1}时{2}分{3}秒", day, hours, minutes, seconds));
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        MainActivity.getInstance().menu.resetDownX();
        switch (v.getId()) {
            case R.id.tv_cat_freq_list:
                MainActivity.getInstance().showFreqListDialog();
                break;
            case R.id.tv_cat_state:
                MainActivity.getInstance().showStepDialog();
                break;
            case R.id.tv_cat_cell:
                MainActivity.getInstance().showDoRfDialog();
                break;
        }
    }
}
